package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.sl.au.entity.BussinessInspection;
import com.sl.au.repository.BussinessInspectionRepository;
import com.sl.au.service.BussinessInspectionService;

@Service
@Transactional
public class BussinessInspectionServiceImpl implements BussinessInspectionService {

	@Autowired
	private BussinessInspectionRepository bussinessInspectionRepository;

	@Override
	public List<BussinessInspection> getPage(int start, int length, String key, String orderName, String dir) {

		Page<BussinessInspection> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "inspectDate");
		pages = bussinessInspectionRepository.findAll(pageable);
		return pages.getContent();
	}

	@Override
	public int getCount(String key) {

		if (!StringUtils.hasText(key))
			return bussinessInspectionRepository.findAll().size();
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "inspectDate");
		return bussinessInspectionRepository.getInspectName(key, pageable).getContent().size();
	}

	@Override
	public List<BussinessInspection> getInspections(int start, int length, String key, String orderName, String dir) {

		Page<BussinessInspection> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "inspectDate");
		pages = bussinessInspectionRepository.getInspectName(key, pageable);
		return pages.getContent();
	}

	@Override
	public List<BussinessInspection> getInspectionsByCheckId(int start, int length, String key, String orderName,
			String dir) {
		Page<BussinessInspection> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "inspectDate");
		pages = bussinessInspectionRepository.getInspectsByCheckId(key, pageable);
		return pages.getContent();
	}

	@Override
	public List<BussinessInspection> findByTimeAndUncheck(int start, int length, String key, String orderName,
			String dir) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "inspectDate");
		return bussinessInspectionRepository.getChecksByTime(key, new Date(), pageable).getContent();
	}

	@Override
	public List<BussinessInspection> findByCheck(int start, int length, String key, String orderName, String dir) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "inspectDate");
		return bussinessInspectionRepository.getChecksByHistory(key, pageable).getContent();
	}

	@Override
	public void save(BussinessInspection bussinessInspection) {
		bussinessInspectionRepository.save(bussinessInspection);

	}

	@Override
	public void deleteById(String id) {
		bussinessInspectionRepository.delete(bussinessInspectionRepository.findOne(id));

	}

	@Override
	public BussinessInspection findById(String id) {

		return bussinessInspectionRepository.findOne(id);
	}

	@Override
	public void update(String id) {

		// bussinessInspectionRepository.saveAndFlush(bussinessInspectionRepository.findOne(id));

	}

	@Override
	public List<BussinessInspection> findByIds(String inspectIds) {
		if (inspectIds != null && inspectIds.length() > 0) {
			String[] ids = inspectIds.split(";");
			List<String> list = new ArrayList<>();
			for (int i = 0; i < ids.length; i++) {
				list.add(ids[i]);
			}
			return bussinessInspectionRepository.findAll(list);
		} else {
			return Collections.emptyList();
		}

	}

}
